package org.jeecg.modules.lab.items.record.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.modules.lab.entrust.entity.LabEntrustItems;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordPerson;
import org.jeecg.modules.lab.entrust.entity.LabItemsRecordSample;
import org.jeecg.modules.lab.items.temp.entity.LabItemsTempPerson;
import org.jeecg.modules.lab.items.record.mapper.LabItemsRecordPersonMapper;
import org.jeecg.modules.lab.items.record.service.ILabItemsRecordPersonService;
import org.jeecg.modules.lab.items.temp.mapper.LabItemsTempPersonMapper;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Basic;
import org.jeecg.modules.lab.items.temp2.entity.LabItemsTemp2Person;
import org.jeecg.modules.lab.items.temp2.mapper.LabItemsTemp2PersonMapper;
import org.jeecg.modules.lab.items.vo.LabRecordPer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


/**
 * @Description: 项目检测记录_人员信息
 * @Author: jeecg-boot
 * @Date: 2023-10-08
 * @Version: V1.0
 */
@Service
public class LabItemsRecordPersonServiceImpl extends ServiceImpl<LabItemsRecordPersonMapper, LabItemsRecordPerson> implements ILabItemsRecordPersonService {

    @Autowired
    private LabItemsTempPersonMapper labItemsTempPersonMapper;

    @Autowired
    private LabItemsRecordPersonMapper labItemsRecordPersonMapper;

    @Autowired
    private LabItemsTemp2PersonMapper labItemsTemp2PersonMapper;


    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertPersonData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String task_item_id= labEntrustItems.getId();
        String item_id = labEntrustItems.getItemId();
        QueryWrapper<LabItemsTempPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("item_id", item_id)
                .orderByAsc("field_sort");
        List<LabItemsTempPerson> labItemsTempNormList = labItemsTempPersonMapper.selectList(queryWrapper);


        for (LabItemsTempPerson labItemsTempPerson : labItemsTempNormList) {
            // TODO: 处理每个实例 labItemsTempNorm
            LabItemsRecordPerson labItemsRecordPerson = new LabItemsRecordPerson();
            labItemsRecordPerson.setTaskId(task_id);
            labItemsRecordPerson.setTaskItemId(task_item_id);
            labItemsRecordPerson.setItemId(item_id);
            labItemsRecordPerson.setFieldName(labItemsTempPerson.getFieldName());
            labItemsRecordPerson.setFieldCode(labItemsTempPerson.getFieldCode());
            labItemsRecordPerson.setFieldValue(labItemsTempPerson.getFieldValue());
            labItemsRecordPerson.setFieldSort(labItemsTempPerson.getFieldSort());
            labItemsRecordPerson.setFieldType(labItemsTempPerson.getFieldType());
            labItemsRecordPerson.setFieldInput(labItemsTempPerson.getFieldInput());
            labItemsRecordPerson.setFieldRow(labItemsTempPerson.getFieldRow());
            labItemsRecordPerson.setFieldCol(labItemsTempPerson.getFieldCol());
            labItemsRecordPerson.setFieldRowspan(labItemsTempPerson.getFieldRowspan());
            labItemsRecordPerson.setFieldColspan(labItemsTempPerson.getFieldColspan());
            labItemsRecordPerson.setSynFlag("0");
            labItemsRecordPersonMapper.insert(labItemsRecordPerson);
            //        获取到mybatis里面的sql语句

        }
    }

    @Override
    public List<LabRecordPer> handlePerData(String entrustId,String taskItemId,String itemId) {
        List<LabRecordPer> recordPerList = labItemsRecordPersonMapper.getRecordPerList(entrustId, itemId);
        QueryWrapper<LabItemsRecordPerson> queryWrapper = new QueryWrapper<>();
        for (LabRecordPer labRecordPer : recordPerList) {
            queryWrapper.clear();
            if(!"".equals(taskItemId) && taskItemId !=null){
                queryWrapper.eq("task_item_id",taskItemId);
            }
            queryWrapper.eq("field_row", labRecordPer.getPId())
                    .eq("task_id", entrustId)
                    .eq("item_id", itemId)
                    .orderByAsc("field_sort");
            List<LabItemsRecordPerson> itemsRecordPersonList = labItemsRecordPersonMapper.selectList(queryWrapper);
            labRecordPer.setItemsRecordPersonList(itemsRecordPersonList);
        }
        return recordPerList;
    }

    @Override
    public List<LabItemsRecordPerson> handlePerDataApp(String entrustId,String taskItemId, String itemId) {

        QueryWrapper<LabItemsRecordPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId)
                .eq("task_item_id",taskItemId)
                .ne("field_input", "0")
                .orderByAsc("field_sort");
        List<LabItemsRecordPerson> itemsRecordPersonList = labItemsRecordPersonMapper.selectList(queryWrapper);
        return itemsRecordPersonList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdate(String entrustId, String itemId, List<LabRecordPer> recordPerList) {
        //1、先根据委托/监督Id、项目id条件删除数据
        QueryWrapper<LabItemsRecordPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.clear();
        queryWrapper.eq("task_id", entrustId)
                .eq("item_id", itemId);
        labItemsRecordPersonMapper.delete(queryWrapper);
        //2、重新插入数据
        if (recordPerList != null && recordPerList.size() > 0) {
            for (LabRecordPer recordPers : recordPerList) {
                for (LabItemsRecordPerson person : recordPers.getItemsRecordPersonList()) {
                    labItemsRecordPersonMapper.insert(person);
                }
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void saveOrUpdateApp(String entrustId, String itemId, List<LabItemsRecordPerson> itemsRecordPersonList) {
        //1、更新人员数据
        if (itemsRecordPersonList != null && itemsRecordPersonList.size() > 0) {
            for (LabItemsRecordPerson person : itemsRecordPersonList) {
                labItemsRecordPersonMapper.updateById(person);
            }
        }
    }

    /**
     * 根据 委托id和项目id
     *
     * @param entrustId
     * @param itemId
     * @return
     */
    @Override
    public List<LabItemsRecordPerson> byEntrustItemId(String entrustId, String itemId) {
        QueryWrapper<LabItemsRecordPerson> objectQueryWrapper = new QueryWrapper<>();
        objectQueryWrapper.eq("item_id", itemId)
                .eq("field_input", "1")
                .eq("task_id", entrustId);
        return labItemsRecordPersonMapper.selectList(objectQueryWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void kyInsertPersonData(LabEntrustItems labEntrustItems) {
        String task_id = labEntrustItems.getEntrustId();
        String item_id = labEntrustItems.getItemId();
        String task_item_id = labEntrustItems.getId();

        //先删除历史数据
        QueryWrapper<LabItemsRecordPerson> wrapper = new QueryWrapper<>();
        wrapper.eq("item_id",item_id);
        labItemsRecordPersonMapper.delete(wrapper);
        QueryWrapper<LabItemsTemp2Person> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("items_id", item_id).orderByAsc("field_sort");
        List<LabItemsTemp2Person> labItemsTemp2personList = labItemsTemp2PersonMapper.selectList(queryWrapper);
        int size = labItemsTemp2personList.size();
        if (size > 0) {
            for (int i = 0; i < size; i++) {
                // 计算整数
                int row = i / 2;
                // 计算余数
                int clo = i % 2;
                LabItemsTemp2Person person = labItemsTemp2personList.get(i);
                String field_name = person.getFieldName();
                //插入名称列
                LabItemsRecordPerson persona = new LabItemsRecordPerson();
                persona.setTaskId(task_id);
                persona.setItemId(item_id);
                persona.setTaskItemId(task_item_id);
                persona.setFieldName(field_name);
                persona.setFieldInput("0");
                persona.setFieldSort(i + 1);
                persona.setFieldType("String");
                persona.setFieldRow(row);
                persona.setFieldCol(clo * 2 + 1);
                persona.setFieldRowspan(0);
                persona.setFieldColspan(0);
                persona.setSynFlag("0");
                labItemsRecordPersonMapper.insert(persona);
                //插入输入列
                LabItemsRecordPerson personb = new LabItemsRecordPerson();
                personb.setTaskId(task_id);
                personb.setItemId(item_id);
                personb.setTaskItemId(task_item_id);
                personb.setFieldName(field_name);
                if("复核人".equals(field_name)){
                    personb.setFieldCode("audit_person");
                }else if("实验人".equals(field_name)){
                    personb.setFieldCode("check_person");
                }
                personb.setFieldInput("1");
                personb.setFieldSort(i + 2);
                personb.setFieldType("String");
                personb.setFieldRow(row);
                personb.setFieldCol(clo * 2 + 2);
                personb.setFieldRowspan(0);
                personb.setFieldColspan(0);
                personb.setSynFlag("0");
                labItemsRecordPersonMapper.insert(personb);
            }
        } else {

        }
    }

    @Override
    public void deleteByTaskId(String taskId) {
        labItemsRecordPersonMapper.deleteByTaskId(taskId);
    }
}
